/**
 * 渠道今日头条推广
 *
 * @author linfeng
 * @since 2020/11/21 15:27
 */
layui.extend({
    zmnExportUtil: '{/}/static/js/zmn.excel.util'
}).define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate', 'zmnExportUtil'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    let zmnExportUtil =layui.zmnExportUtil;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-170',
        done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');

            //添加按钮
            let hasHelpBtn = $('[lay-event=CUSTOM_HELP]').length !== 0;
            if (!hasHelpBtn) {
                $(".layui-table-tool-self").prepend(toolbar_help.innerHTML);
            }
        }
    });
    laypage.render(layui.$.extend({limits:[10, 20, 30, 40, 50,100]},zmnPage.laypageOptions(json)));

    //查询条件重置
    form.on('submit(zmn-form-reset)', function (obj) {
        form.val("zmn-form-filter", {
            "companyName": ""
            ,"accountNameOrId": ""
            ,"status": ""
        });
        return false;
    });

    var ids = new Array();

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        let items = obj.config.data;
        ids = zmnPage.layIdsChecked(items, "relateId");

        //导出excel
        if(obj.event=='CUSTOM_EXPORT'){
            zmnExportUtil.doExportExcel('zmn-table','渠道今日头条推广');
            return ;
        }

        if (obj.event === 'CUSTOM_HELP') {
            tips(help_content.innerHTML, this);
        }

        if (obj.event != "start" && obj.event != "stop") {
            return;
        }
        if (ids.length === 0) {
            return layer.alert("请选择至少一选择一条数据！");
        }
        zmnPage.layStartStop(obj, "/channel/promotion/updateStatus.action", ids.join(","));
    });

    /**
     * 封装的tips，适配laytable的配色，点击提示框外会关闭提示（layer.tips无法点击空白处隐藏tips）
     * @param html 提示内容
     * @param ele 绑定对象
     */
    function tips(html, ele) {
        let style = document.createElement("style");
        style.appendChild(document.createTextNode("    /*帮助提示框*/\n" +
            "    body .zmn-tips {\n" +
            "\n" +
            "    }\n" +
            "\n" +
            "    body .zmn-tips .layui-layer-content {\n" +
            "        color: #0C0C0C;\n" +
            "        background-color: #e6e6e6;\n" +
            "        padding-right: 3px;\n" +
            "        width: 396px;\n" +
            "        height: 300px;\n" +
            "        overflow-x: hidden;\n" +
            "        overflow-y: scroll;\n" +
            "    }\n" +
            "\n" +
            "    body .zmn-tips .layui-layer-content::-webkit-scrollbar-track {\n" +
            "        background-color: #e6b31a;\n" +
            "    }\n" +
            "\n" +
            "    body i.layui-layer-TipsB, i.layui-layer-TipsT, i.layui-layer-TipsL, i.layui-layer-TipsR, i.layui-layer-TipsG {\n" +
            "        border-right-color: #e6e6e6;\n" +
            "    }\n" +
            "\n" +
            "    body .layui-layer-tips i.layui-layer-TipsB, .layui-layer-tips i.layui-layer-TipsT {\n" +
            "        border-right-color: #e6e6e6;\n" +
            "    }"));
        let head = document.getElementsByTagName("head")[0];
        head.appendChild(style);
        layui.layer.open({
            type: 4
            , closeBtn: false
            , shade: 0.0001
            , tips: 1
            , skin: 'zmn-tips'
            , shadeClose: true
            , content: [html, ele]
        });
    }

    //对外暴露的接口
    exports('channel_promotion_list', {});
});


//显示描述
function show_tips(flag, tips, obj){
    //小tips
    //如果没有权限则提示
    if (!flag) {
        layer.tips(tips,obj,{
            tips:[1,'#3595CC'],
            time:3000
        })
    }
}

function displayHiddenData(id, colName, permitFlag) {
    if (!permitFlag) {
        return false;
    }

    let map = {};
    json.items.forEach(row => {
        map[row.relateId] = {secretKey: row.secretKey, token: row.token}
    });

    if (colName == 'secretKey') {
        $('.'+id+'_'+colName).text(map[id].secretKey);
    } else if (colName == 'token'){
        $('.'+id+'_'+colName).text(map[id].token);
    }

    $('a.'+id+'_'+colName).remove();
}

